package org.hawksee.javase.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;

import org.hawksee.core.tools.Registrator;
import org.hawksee.javase.MainFrame;

public class RegistratorMenu extends JMenu implements ActionListener
{
  public static final long serialVersionUID = 0;
  private static final String ACTION_REGISTER = "Register";
  private static final String ACTION_SET_ALLOWED_MOVE = "Set Allowed Move";
  private static final String ACTION_SET_POINTS_MATCHED = "Set Points Matched";
  
  private MainFrame mainFrame;
  private Hashtable<JRadioButtonMenuItem, Registrator.Feature> featureChoices;
  
  public RegistratorMenu(MainFrame mainFrame) 
  {
    this.setText("Registrator");
    this.mainFrame = mainFrame;
    this.featureChoices = new Hashtable<JRadioButtonMenuItem, Registrator.Feature>();
    
    JRadioButtonMenuItem radioMenuItem;
    ButtonGroup group;
    
    JMenuItem menuItem;
    menuItem = new JMenuItem(ACTION_REGISTER);
    menuItem.setActionCommand(ACTION_REGISTER);
    menuItem.addActionListener(this);
    this.add(menuItem);
    
    // add a menu group for features
    JMenu menuFeatures = new JMenu("Image Feature");
    group = new ButtonGroup();
    for (Registrator.Feature featureSet : Registrator.Feature.values()) {
      radioMenuItem = new JRadioButtonMenuItem(featureSet.name());
      group.add(radioMenuItem);
      menuFeatures.add(radioMenuItem);
      radioMenuItem.addActionListener(this);
      this.featureChoices.put(radioMenuItem, featureSet);
      if (this.mainFrame.getManager().getRegistrator().getFeature().equals(featureSet)) {
        radioMenuItem.setSelected(true);
      }
    }
    this.add(menuFeatures);
    
    // set allowed move
    menuItem = new JMenuItem(ACTION_SET_ALLOWED_MOVE);
    menuItem.setActionCommand(ACTION_SET_ALLOWED_MOVE);
    menuItem.addActionListener(this);
    this.add(menuItem);
    
    // set points matched
    menuItem = new JMenuItem(ACTION_SET_POINTS_MATCHED);
    menuItem.setActionCommand(ACTION_SET_POINTS_MATCHED);
    menuItem.addActionListener(this);
    this.add(menuItem);
  }
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (this.featureChoices.containsKey(source)) {
      this.mainFrame.getManager().getRegistrator().setFeature(this.featureChoices.get(source));
    } else {
      String actionCmd = e.getActionCommand();
      if (actionCmd == ACTION_REGISTER) {
        this.mainFrame.getManager().register();
      } else if (actionCmd == ACTION_SET_ALLOWED_MOVE) {
        try {
          int allowedMove = Integer.parseInt((String)JOptionPane.showInputDialog(this.mainFrame,
            ACTION_SET_ALLOWED_MOVE,
            ACTION_SET_ALLOWED_MOVE,
            JOptionPane.PLAIN_MESSAGE));
          this.mainFrame.getManager().getRegistrator().setAllowedMove(allowedMove);
        } catch (NumberFormatException nfe) {
        }
      } else if (actionCmd == ACTION_SET_POINTS_MATCHED) {
        try {
          int pointsMatched = Integer.parseInt((String)JOptionPane.showInputDialog(this.mainFrame,
            ACTION_SET_POINTS_MATCHED,
            ACTION_SET_POINTS_MATCHED,
            JOptionPane.PLAIN_MESSAGE));
          this.mainFrame.getManager().getRegistrator().setPointsMatched(pointsMatched);
        } catch (NumberFormatException nfe) {
        }
      }
    }
    this.mainFrame.repaint();
  }
}
